﻿<template>
  <view class="tab-hotel">
    <view class="content-container">
      <view v-if="loading" class="loading-container">
        <text class="loading-text">加载中...</text>
      </view>
      
      <view v-else-if="error" class="error-container">
        <text class="error-text">{{ error }}</text>
        <view class="retry-btn" @tap="loadData">
          <text>重试</text>
        </view>
      </view>
      
      <view v-else-if="dataList.length === 0" class="empty-container">
        <text class="empty-text">暂无民宿信息</text>
      </view>
      
      <view v-else>
        <view 
          class="hotel-item" 
          v-for="(item, index) in dataList" 
          :key="index"
          @tap="onItemClick(item)"
        >
          <view class="hotel-image">
            <image 
              v-if="item.image || item.cover" 
              :src="item.image || item.cover" 
              mode="aspectFill"
              class="real-image"
            />
            <view v-else class="placeholder-image">
              <text class="placeholder-emoji">🏠</text>
            </view>
            <view class="rating-badge" v-if="item.rating">
              <text class="rating-text">{{ item.rating }}分</text>
            </view>
          </view>
          <view class="hotel-info">
            <text class="hotel-title">{{ item.title || item.name || '民宿名称' }}</text>
            <text class="hotel-location" v-if="item.address">
              📍 {{ item.address }}
            </text>
            <text class="hotel-desc">{{ item.description }}</text>
            <view class="hotel-details" v-if="item.contact || item.telephone">
              <text class="detail-item" v-if="item.contact">👤 {{ item.contact }}</text>
              <text class="detail-item" v-if="item.telephone">📞 {{ item.telephone }}</text>
            </view>
            <view class="hotel-footer">
              <view class="price-info">
                <text class="price" v-if="item.price">¥{{ item.price }}</text>
                <text class="price" v-else>价格面议</text>
                <text class="price-unit">/晚</text>
                <text class="vip-price" v-if="item.price2 && item.price2 < item.price">VIP价: ¥{{ item.price2 }}</text>
              </view>
              <view class="book-btn">
                <text>查看详情</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import sheep from '@/sheep';

const loading = ref(false);
const error = ref('');
const dataList = ref([]);

const loadData = async () => {
  try {
    loading.value = true;
    error.value = '';
    
    const city = await sheep.$helper.getCurrentCity();
    const res = await sheep.$api.noAuth.getHomeContentByType('4', city);
    
    if (res && res.content && Array.isArray(res.content)) {
      dataList.value = res.content
        .filter(item => item.state === 1)
        .map(item => ({
          id: item.id,
          title: item.name,
          name: item.aliasName || item.name,
          description: `${item.layout || '户型未知'} · ${item.area || '面积未知'}㎡`,
          content: item.content,
          image: getFirstValidImage(item.pics),
          cover: getFirstValidImage(item.pics),
          url: item.url || null,
          price: item.price || null,
          price2: item.price2 || null,
          rating: item.rating || null,
          location: item.address,
          address: `${item.province || ''}${item.city || ''}${item.district || ''}${item.address || ''}`,
          contact: item.contact,
          telephone: item.telephone,
          area: item.area,
          layout: item.layout,
          floors: item.floors,
          devices: item.devices
        }));
    } else {
      dataList.value = [];
    }
  } catch (err) {
    error.value = '加载失败，请稍后重试';
    dataList.value = [];
  } finally {
    loading.value = false;
  }
};

const getFirstValidImage = (pics) => {
  if (!pics || pics === 'null' || pics === '') return null;
  
  const picArray = pics.split(',').filter(pic => pic && pic !== 'null' && pic.trim() !== '');
  if (picArray.length === 0) return null;
  
  const imagePath = picArray[0].trim();
  if (imagePath.startsWith('http://') || imagePath.startsWith('https://')) {
    return imagePath;
  }
  
  const baseUrl = sheep.$platform.baseUrl || '';
  return baseUrl + (imagePath.startsWith('/') ? imagePath : '/' + imagePath);
};

const onItemClick = (item) => {
  if (item.url) {
    sheep.$router.go(item.url);
  } else if (item.id) {
    uni.navigateTo({
      url: `/pages/hotel-detail?id=${item.id}`
    });
  }
};

onMounted(() => {
  loadData();
});

defineExpose({
  loadData
});
</script>

<style lang="scss" scoped>
$primary-color: #8B4513;
$secondary-color: #DAA520;
$text-dark: #333;
$text-light: #666;

.tab-hotel {
  min-height: 100vh;
  background: #fff;
  padding: 20rpx;

  .loading-container,
  .error-container,
  .empty-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 400rpx;
    
    .loading-text,
    .error-text,
    .empty-text {
      font-size: 28rpx;
      color: $text-light;
      margin-bottom: 20rpx;
    }
    
    .retry-btn {
      padding: 16rpx 32rpx;
      background: $primary-color;
      color: white;
      border-radius: 24rpx;
      font-size: 26rpx;
    }
  }

  .hotel-item {
    background: white;
    border-radius: 16rpx;
    padding: 0;
    margin-bottom: 20rpx;
    box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.05);
    overflow: hidden;
    
    .hotel-image {
      position: relative;
      width: 100%;
      height: 400rpx;
      
      .real-image {
        width: 100%;
        height: 100%;
      }
      
      .placeholder-image {
        width: 100%;
        height: 100%;
        background: linear-gradient(135deg, #f0f0f0 0%, #e0e0e0 100%);
        display: flex;
        align-items: center;
        justify-content: center;
        
        .placeholder-emoji {
          font-size: 60rpx;
        }
      }
      
      .rating-badge {
        position: absolute;
        top: 20rpx;
        right: 20rpx;
        background: rgba(0,0,0,0.7);
        color: white;
        padding: 8rpx 16rpx;
        border-radius: 20rpx;
        
        .rating-text {
          font-size: 24rpx;
          font-weight: bold;
        }
      }
    }
    
    .hotel-info {
      padding: 30rpx;
      
      .hotel-title {
        display: block;
        font-size: 32rpx;
        font-weight: bold;
        color: $text-dark;
        margin-bottom: 10rpx;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      
      .hotel-location {
        display: block;
        font-size: 24rpx;
        color: $text-light;
        margin-bottom: 10rpx;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      
      .hotel-desc {
        display: block;
        font-size: 26rpx;
        color: $text-light;
        margin-bottom: 15rpx;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        white-space: normal;
      }
      
      .hotel-details {
        display: flex;
        gap: 20rpx;
        margin-bottom: 20rpx;
        flex-wrap: wrap;
        
        .detail-item {
          font-size: 24rpx;
          color: $text-light;
          background: #f5f5f5;
          padding: 8rpx 12rpx;
          border-radius: 12rpx;
        }
      }
      
      .hotel-footer {
        display: flex;
        align-items: center;
        justify-content: space-between;
        
        .price-info {
          display: flex;
          flex-direction: column;
          align-items: flex-start;
          
          .price {
            font-size: 32rpx;
            font-weight: bold;
            color: $primary-color;
            display: flex;
            align-items: baseline;
          }
          
          .price-unit {
            font-size: 24rpx;
            color: $text-light;
            margin-left: 8rpx;
          }
          
          .vip-price {
            font-size: 24rpx;
            color: $secondary-color;
            margin-top: 5rpx;
          }
        }
        
        .book-btn {
          padding: 12rpx 24rpx;
          background: linear-gradient(135deg, $primary-color 0%, $secondary-color 100%);
          color: white;
          border-radius: 20rpx;
          font-size: 24rpx;
        }
      }
    }
  }
}
</style> 
